﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class SystemManage_TruckStatusEdit : BasePage
{
    ISyTruckStatusHandler tsManager = new SyTruckStatusHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!(strPermission.Contains("1028") || strPermission.Contains("1032")))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }

            dataBind();
        }
    }
    private void dataBind()
    {

        if (Request["PKID"] != null)
        {
            Sy_TruckStatusQuery query = tsManager.GetTruckStatusByID(int.Parse(Request["PKID"]));
            if (query != null)
            {
                txtStatusName.Text = query.StatusName;
                txtHKStatusName.Text = query.HKStatusName;
                txtENGStatusName.Text = query.ENGStatusName;
                txtSequence.Text = query.Sequence.ToString();
                txtInterval.Text = query.Interval.ToString();
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Sy_TruckStatusQuery tsQuery = new Sy_TruckStatusQuery();

        tsQuery.HKStatusName = txtHKStatusName.Text.Trim();
        tsQuery.ENGStatusName = txtENGStatusName.Text.Trim();
        tsQuery.StatusName = txtStatusName.Text.Trim();
        tsQuery.Sequence = int.Parse(txtSequence.Text.Trim());
        tsQuery.Interval = decimal.Parse(txtInterval.Text.Trim());
        tsQuery.Status = 1;
        tsQuery.LastModUser = int.Parse(Session["UserID"].ToString());
        tsQuery.LastModDate = DateTime.Now;

        try
        {
            if (Request["PKID"] != null)
            {
                Sy_TruckStatusQuery tmp = tsManager.GetTruckStatusByID(int.Parse(Request["PKID"]));
                if (tmp == null)
                {
                    Jscript.Alert(Resources.Messages.UpdateRecordNotExists, this.Page);
                }
                else
                {
                    tsQuery.PKID = tmp.PKID;
                    if (tmp.Sequence != tsQuery.Sequence && isExistsSeq(tsQuery.Sequence) == true)
                    {
                        Jscript.Alert(Resources.Messages.SequenceExists, this.Page);
                        return;
                    }
                    if(tsManager.UpdateTruckStatus(tsQuery))
                    {
                        Jscript.AlertAndRedirect(Resources.Messages.UpdateSuccess, "TruckStatusList.aspx", this.Page);
                    }
                    else
                    {
                        Jscript.Alert(Resources.Messages.UpdateFailed, this.Page);
                    }
                }
            }
            else
            {
                if (isExistsSeq(tsQuery.Sequence) == true)
                {
                    Jscript.Alert(Resources.Messages.SequenceExists, this.Page);
                    return;
                }
                if (tsManager.AddTruckStatus(tsQuery))
                    {
                        Jscript.AlertAndRedirect(Resources.Messages.AddSuccess, "TruckStatusList.aspx", this.Page);
                    }
                    else
                    {
                        Jscript.Alert(Resources.Messages.AddFailed, this.Page);
                    }
            }
        }
        catch
        {
            Jscript.Alert(Resources.Messages.SaveFailed, this.Page);
        }
        
    }

    private bool isExistsSeq(int seq)
    {
        string sqlStr = "SELECT * FROM Sy_TruckStatus WHERE Sequence=@Sequence AND Status = @Status";
        SqlParameter[] parms = new SqlParameter[2];
        parms[0] = new SqlParameter("@Sequence", seq.ToString());
        parms[1] = new SqlParameter("@Status", "1");

        DataTable dt = tsManager.GetList(sqlStr, parms);

        if (dt != null && dt.Rows.Count > 0)
        {
            return true;
        }

        return false;
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("TruckStatusList.aspx");
    }
}
